/**
 * 
 */
package com.yst.metho.web.application.components;

import nextapp.echo2.app.Button;
import nextapp.echo2.app.Column;
import nextapp.echo2.extras.app.AccordionPane;
import nextapp.echo2.extras.app.layout.AccordionPaneLayoutData;

import com.yst.metho.web.application.SuiviProdApp;
import com.yst.metho.web.application.interfaces.IItemViewWeb;
import com.yst.metho.web.application.interfaces.IModuleController;

/**
 * Composant qui affiche une zone d'actions utilisateurs. Les actions sont
 * triees par categorie.
 * 
 * @author perso
 * 
 */
public class AreaViewPane extends AccordionPane
{

	/**
	 * Reference les styles utilisespar le composant.
	 */
	private static final String AERA_VIEW_STYLE = "AreaViewControlColumn";

	private static final String AERA_VIEW_BUTTON_STYLE = "AreaViewControlButton";

	/**
	 * Initialisation du composant. La methode interroge le controleur de module
	 * pour recuperer toutes les vues disponibles.
	 * 
	 */
	public void buildArea()
	{
		// Recuperation de tous les items et construction du composant.
		IModuleController moduleCtrl = SuiviProdApp.getApp().getModuleCtrl();

		// for (IItemViewWeb currentView : moduleCtrl.getItemViews())
		IItemViewWeb currentView;
		for (int i = 0; i < moduleCtrl.getItemViews().size(); i++)
		{
			currentView = (IItemViewWeb) moduleCtrl.getItemViews().get(i);
			buildView(currentView);
		}
	}

	/**
	 * @param currentView
	 */
	public void buildView(IItemViewWeb currentView)
	{
		Column controlsColumn;
		boolean alreadyPresent = false;
		if (currentView.canDo())
		{
			controlsColumn = (Column) getComponent(currentView.getCategory());
			if (null == controlsColumn)
			{
				controlsColumn = new Column();
				controlsColumn.setId(currentView.getCategory());
				AccordionPaneLayoutData layoutData = new AccordionPaneLayoutData();
				layoutData.setTitle(currentView.getCategory());
				controlsColumn.setLayoutData(layoutData);
				controlsColumn.setStyleName(AERA_VIEW_STYLE);
				add(controlsColumn);
			}
			for (int i = 0; i < controlsColumn.getComponentCount(); i++)
			{
				if (currentView.getName().equals(((Button) controlsColumn.getComponent(i)).getText()))
				{
					alreadyPresent = true;
				}
			}
			if (!alreadyPresent)
			{
				Button btn = new Button(currentView.getName());
				btn.setStyleName(AERA_VIEW_BUTTON_STYLE);
				btn.addActionListener(currentView);
				controlsColumn.add(btn);
			}
		}
	}

	/**
	 * @param currentView
	 */
	public void removeView(IItemViewWeb currentView)
	{
		Column controlsColumn;
		if (currentView.canDo())
		{
			controlsColumn = (Column) getComponent(currentView.getCategory());
			if (null != controlsColumn)
			{
				for (int i = 0; i < controlsColumn.getComponentCount(); i++)
				{
					if (currentView.getName().equals(((Button) controlsColumn.getComponent(i)).getText()))
					{
						controlsColumn.remove(i);
					}
				}
				if (controlsColumn.getComponentCount() == 0)
				{
					remove(controlsColumn);
				}
			}

		}
	}
}
